package com.panopset;

import java.io.StringWriter;

import org.joda.time.DateTime;


/**
 * DateTime utilities.
 *
 * @author Karl Dinwiddie
 *
 */
public class Timer {

    /**
     *
     * @param args Not used.
     */
    public static void main(String... args) {
        System.out.println(Timer.getInstance().getCurrentDateString());
    }

    private DateTime currentDateTime;
    public DateTime getCurrentDateTime() {
        if (currentDateTime == null) {
            currentDateTime = new DateTime();
        }
        return currentDateTime;
    }

    private String currentDateString;
    private String getCurrentDateString() {
        if (currentDateString == null) {
            StringWriter sw = new StringWriter();
            sw.append("" + getCurrentDateTime().getYear());
            sw.append("" + Util.pad("" + getCurrentDateTime().getMonthOfYear(), '0', 2, true));
            sw.append("" + Util.pad("" + getCurrentDateTime().getDayOfMonth(), '0', 2, true));
            currentDateString = sw.toString();
        }
        return currentDateString;
    }

    /**
     * Based on SingletonHolder inner class by Bill Pugh.
     *
     *
     * <h5>References</h5>
     * <ul>
     * <li>
     * <a href="http://en.wikipedia.org/wiki/Singleton_pattern">
     * http://en.wikipedia.org/wiki/Singleton_pattern </a></li>
     * </ul>
     *
     */
    private static final class Singleton {
        /**
         * Instance variable.
         */
        private static final Timer INSTANCE = new Timer();

        /**
         * Private constructor.
         */
        private Singleton() {
            // Prevent instantiation.
        }
    }

    /**
     * @return static MathUtil instance.
     */
    public static Timer getInstance() {
        return Singleton.INSTANCE;
    }

    /**
     * Private singleton constructor.
     */
    private Timer() {
        // Prevent outside instantiation.
    }
}
